package org.javascud.extensions.cxf.loadbalance;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.javascud.extensions.cxf.LoadBalanceStrategy;
import org.javascud.extensions.cxf.service.Hello;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class HelloLoadBalanceAndFailOverClientByXML
{
    public static void main(String[] args)
    {
        ClassPathXmlApplicationContext context
                = new ClassPathXmlApplicationContext(new String[]
                {"org/javascud/extensions/cxf/loadbalance/loadbalance_fail.xml"});
        Hello client = (Hello) context.getBean("helloClient");

        LoadBalanceStrategy strategy = (LoadBalanceStrategy) context.getBean("randomAddresses");

        Client myclient = ClientProxy.getClient(client);
        String address = myclient.getEndpoint().getEndpointInfo().getAddress();

        System.out.println(address);

        for(int i=1; i<=10; i++)
        {
            String result1 = client.sayHello("Felix" +i);
            System.out.println("Call " + i +": " + result1);

            int left = strategy.getAlternateAddresses(null).size();
            System.out.println("================== left " + left + " ===========================");
        }


    }
}
